#include "dvbinner.h"
#include "builderplugin.h"
#include "dvbfun4plugin.h"

int DvbBuilderTunerLock(builder_tuner_t* pstFreq)
{
	int s32Ret = -1;
	dvb_tuner_t stTunerInfo;
	dvb_tuner_status_t stTunerStatus;

	mwDebug("DvbBuilderTunerLock() - entry, pstFreq = %p\r\n", pstFreq);
	if(pstFreq)
	{
		stTunerInfo.frequency = pstFreq->m_u32Freq;
		stTunerInfo.symbol_rate = pstFreq->m_u32Sym;
		stTunerInfo.modulation = pstFreq->m_u32Mod;
		stTunerInfo.tunertype = pstFreq->m_u32Tunertype;
		stTunerInfo.bandwidth = pstFreq->m_u32Bandwidth;
		//s32Ret = DvbTunerLock(&stTunerInfo, NULL, DvbTunerLockFinish, 1);

		stTunerStatus.frequency = stTunerInfo.frequency;
		stTunerStatus.symbol_rate = stTunerInfo.symbol_rate;
		stTunerStatus.modulation = stTunerInfo.modulation;
		stTunerStatus.bandwidth = stTunerInfo.bandwidth;
		stTunerStatus.snr = 0;
		stTunerStatus.level = 0;
		stTunerStatus.bLock = 0;
		s32Ret = DVBTunerForceLock(&stTunerInfo);
		
		mwDebug("DvbTunerForceLock() - return, s32Ret = %d\r\n", s32Ret);
		if(s32Ret == 0)
		{
			s32Ret = DVBGetSignalStatus(&stTunerStatus);
			mwDebug("DvbGetSignalStatus() - return, s32Ret = %d\r\n", s32Ret);
			if(s32Ret != 0)
			{
				mwError("DvbBuilderTunerLock() - get status failed\r\n");
				stTunerStatus.bLock = 0;
			}
		}
		DvbTunerLockFinish(NULL, &stTunerStatus);
	}
	mwDebug("DvbBuilderTunerLock() - return, s32Ret = %d\r\n", s32Ret);

	return s32Ret;
}

int DvbBuilderSetFilter(unsigned int* phFilter, unsigned short u16Pid, unsigned char* pu8Data, unsigned char* pu8Mask, unsigned int u32Depth)
{
	int s32Ret = -1;
	filter_param_t stParam;
	dvb_handle_t hfilter;
	unsigned short au16SecByte[16];
	unsigned int i;

	mwDebug("DvbBuilderSetFilter() - entry, phFilter = %p, pu8Data = %p, pu8Mask = %p, u32Depth = %d\r\n", phFilter, pu8Data, pu8Mask, u32Depth);
	if(phFilter && pu8Data && pu8Mask && u32Depth < 16)
	{
		memset(&stParam, 0, sizeof(stParam));
		memset(au16SecByte, 0, sizeof(au16SecByte));
		for(i=0; i<u32Depth; i++)
		{
			au16SecByte[i] = (pu8Mask[i]<<8)|pu8Data[i];
		}
		stParam.m_pu16SectByte = au16SecByte;
		stParam.m_u8FilterDepth = u32Depth;
		stParam.m_u8CrcCheck = 1;
		stParam.m_u16Pid = u16Pid;
		stParam.m_pfCb = DvbBuilderDataReceived;
		s32Ret = mwDmxStartFilter(&stParam, &hfilter);
		mwDebug("mwDmxStartFilter() s32Ret = %d\r\n", s32Ret);
		if(s32Ret == 0)
			*phFilter = hfilter;
		else
			*phFilter = BUILDER_INVALID_FILTER;
	}
	else
	{
		mwError("DvbBuilderSetFilter() - receive error parameter, phFilter = %p, pu8Data = %p, pu8Mask = %p, u32Depth = %d\r\n", phFilter, pu8Data, pu8Mask, u32Depth);
	}
	mwDebug("DvbBuilderSetFilter() - return, s32Ret = %d\r\n", s32Ret);

	return s32Ret;
}

int DvbBuilderReleaseFilter(unsigned int hFilter)
{
	int s32Ret = -1;
	
	mwDebug("DvbBuilderReleaseFilter() - entry, hFilter = %d\r\n", hFilter);
	if(hFilter != BUILDER_INVALID_FILTER)
	{
		s32Ret = mwDmxStopFilter(hFilter);
	}
	mwDebug("DvbBuilderReleaseFilter() - return, s32Ret = %d\r\n", s32Ret);
	return s32Ret;
}

int DvbBuilderWriteNvm(unsigned char* pu8Data, unsigned int u32Len, unsigned int u32Offset)
{
	int s32Ret = -1;

	mwDebug("DvbBuilderWriteNvm() - entry, pu8Data= %p, u32Len = %d, u32Offset = %d\r\n", pu8Data, u32Len, u32Offset);
	
	mwDebug("DvbBuilderWriteNvm() - return, s32Ret = %d\r\n", s32Ret);

	return s32Ret;
}

int DvbBuilderReadNvm(unsigned char* pu8Data, unsigned int u32Len, unsigned int u32Offset)
{
	int s32Ret = -1;

	mwDebug("DvbBuilderReadNvm() - entry, pu8Data= %p, u32Len = %d, u32Offset = %d\r\n", pu8Data, u32Len, u32Offset);
	
	mwDebug("DvbBuilderReadNvm() - return, s32Ret = %d\r\n", s32Ret);

	return s32Ret;
}

void DvbBuilderDebug(char* ps8String)
{
	mwDebug("icastbuilder-plugin:%s", ps8String);
}

void* DvbBuilderMemAlloc(unsigned int pu32Size)
{
	return mwOsAllocMemory(pu32Size);
}

void DvbBuilderMemFree(void* pPointer)
{
	mwOsFreeMemory(pPointer);
}

int DvbBuilderConvertString(unsigned char *pString, unsigned short len, unsigned char *lanCode)
{
	return mwConvertString(pString, len, lanCode);
}

